package com.redis.springtest;

import com.redis.springtest.config.RedisConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;

import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2020-07-27 下午2:30
 */
public class AppRedisSet {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(RedisConfig.class);
        final RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);

        final SetOperations setOperations = redisTemplate.opsForSet();
        Set set = null;
        // 将元素加入列表中
        redisTemplate.boundSetOps("set1").add("v1", "v2", "v3", "v4", "v5", "v6");
        redisTemplate.boundSetOps("set2").add("v0", "v2", "v4", "v6", "v8", "v10");
        // 求集合长度
        System.out.println(setOperations.size("set1"));
        // 求差集
        set = setOperations.difference("set1", "set2");
        // 求并集
        set = setOperations.intersect("set1", "set2");
        // 判断是否是集合中的元素
        final Boolean member = setOperations.isMember("set1", "v3");
        // 获取集合所有元素
        set = setOperations.members("set1");
        // 从集合中随机弹出一个元素
        final Object pop = setOperations.pop("set1");
        System.out.println(pop);
        // 随机从集合中获取一个元素
        final Object ran = setOperations.randomMember("set1");
        final List randomMembers = setOperations.randomMembers("set1", 2L);
        // 删除一个集合的元素，参数可以是多个
        setOperations.remove("set1", "v1");
        // 求两个集合的并集
        setOperations.union("set1", "set2");
        // 求两个集合的差集, 并保存到diff_set中
        setOperations.differenceAndStore("set1", "set2", "diff_set");
        // 求两个集合的交集，并保存到inter_set中
        setOperations.intersectAndStore("set1", "set2", "inter_set");
        // 求两个集合额并集,并保存到union_set中
        setOperations.unionAndStore("set1", "set2", "union_set");
    }
}
